#include "BondDetailPrcInter.h"
#include "BondDetailData.h"
#include "uam/UserAccountManager.h"
#include "bondlib/SSCFETSQuote.h"
#include "BondDetailDlg.h"
#include <QSpacerItem>
#include <QStylePainter>
#include <core/StyleMgr.h>
#include <QVBoxLayout>


BondDetailPrcInter::BondDetailPrcInter(QWidget* p)
    : QFrame(p)
{
	setMouseTracking(true);


}

void BondDetailPrcInter::setDetailData(QSharedPointer<DetailData> data)
{
    m_data = data;
    connect(m_data.get(), &DetailData::crossMarketQuoteChanged, this, &BondDetailPrcInter::onUpdateUi);
}

void BondDetailPrcInter::paintEvent(QPaintEvent* event)
{
	QStylePainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing);
	painter.fillRect(rect(), StyleMgr::instance().color("BasicPlate"));
	
	QRect r = rect();
	r.setHeight(24);
	QString interBond = tr("跨市场债券");

	QFontMetrics fm(font());
	int widthTxt = fm.horizontalAdvance(interBond);

	int titleX = r.x() + 8;

	QRect titleRect;
	titleRect.setRect(titleX, r.y()+ 7 , widthTxt, r.height());
	painter.setPen(StyleMgr::instance().color("TextNormal"));
	painter.drawText(titleRect, interBond, QTextOption(Qt::AlignCenter));
	
	int w = 16;
	int firstX = titleX + widthTxt + w;
	int singleX, numX,homeX;
	int restX = 0;
	int quoteX = 0;
	m_rects.clear();
	QFont f = font();
	if (m_data->crossMarketData().size() == 0)
	{//无跨市场债券
		QString strText = tr("无跨市场债券");
		widthTxt = fm.horizontalAdvance(strText);
		titleRect.setRect(r.width() / 2 + 7, r.y()+7, widthTxt, r.height());
		painter.setPen(StyleMgr::instance().color("Highlight"));
		painter.drawText(titleRect, strText, QTextOption(Qt::AlignCenter));
	}
	else
	{
		int count = m_data->crossMarketData().size();
		if (count == 1) singleX = 0;
		else if (count == 2) singleX = (r.width() - firstX) / 2;
		if (count != 2 && count != 1) return;

		for (int i = 0; i < count; i++)
		{	
			CCrossMarketInfo mkt = m_data->crossMarketData()[i];
			QString szCode = QString("%1(%2)").arg(QString::fromLocal8Bit(mkt.m_sShorName)).arg(QString::fromLocal8Bit(mkt.m_sCombCode)); //显示跨市场债券

			firstX += restX;

			QFontMetrics fmInterBond(font());
			if (m_hoverIndex == i)
			{
				f.setBold(false);
				f.setUnderline(true);
			}
			else
			{
				f.setBold(true);
				f.setUnderline(false);
			}
			painter.setFont(f);
			int interBondwidthTxt = fm.horizontalAdvance(szCode) + 8; //"("矩形像素宽度为4
			m_rects.push_back({ firstX, r.y() + 7, interBondwidthTxt, r.height() });
			
			painter.setPen(StyleMgr::instance().color("Highlight"));
			painter.drawText(m_rects[i], szCode, QTextOption(Qt::AlignCenter));

			QFontMetrics fmQuote(font());
			int n = (int)(mkt.m_vtCorpId.size());

			f.setUnderline(false);
			f.setBold(true);
			painter.setFont(f);
			
			if (n == 0)
			{
				QString szQuote = tr("无报价");
				int interBondQuoteWidth = fmQuote.horizontalAdvance(szQuote);
				if (count == 1)
				{
					quoteX = r.width() - 9 - 36;
				}
				else
				{
					quoteX = firstX + singleX - 9 - 36;
				}
				int X = r.x() + quoteX;

				titleRect.setRect(X, r.y() + 7, interBondQuoteWidth, r.height());
				painter.setPen(StyleMgr::instance().color("SupportingWord"));
				painter.drawText(titleRect, szQuote, QTextOption(Qt::AlignCenter));
			}
			else
			{
				//有 N 家报价
				QString szQuote2 = QString("%1").arg(n);
				int nWidth = fmQuote.horizontalAdvance(szQuote2);
				if (count == 1)
				{
					numX = r.x() + r.width() - 31;
				}
				else
				{
					numX = firstX + singleX - 31;
				}
				titleRect.setRect(numX, r.y() + 7, nWidth, r.height());
				painter.setFont(f);
				painter.setPen(StyleMgr::instance().color("Highlight"));
				painter.drawText(titleRect, szQuote2, QTextOption(Qt::AlignCenter));

				//显示N家
				QString szQuote3 = tr("家");
				int homeWidth = fmQuote.horizontalAdvance(szQuote3);
				if (count == 1)
				{
					homeX = r.x() + r.width() - 21;
				}
				else
				{
					homeX = firstX + singleX - 21;
				}
				titleRect.setRect(homeX, r.y() + 7, homeWidth, r.height());
				painter.setPen(StyleMgr::instance().color("SupportingWord"));
				painter.drawText(titleRect, szQuote3, QTextOption(Qt::AlignVCenter));
			}
			restX = singleX;
		}
	}
}

void BondDetailPrcInter::mousePressEvent(QMouseEvent* event)
{
	if (event->button() == Qt::LeftButton)
	{
		for (size_t i = 0; i < m_rects.size(); i++)
		{
			if (m_rects[i].contains(event->pos()))
			{
				CCrossMarketInfo mkt = m_data->crossMarketData()[i];
				QString strcombKey = QString("%1.%2").arg(QString::fromLocal8Bit(mkt.m_sBondKey)).arg(QString::fromLocal8Bit(mkt.m_sListedMarket)); 
				
				QByteArray combKey = strcombKey.toLatin1();
				const char* str = combKey.data();

				m_data->getBondDetailDlg()->setCombondKey(str);
				update();
			}
		}
	}
}

void BondDetailPrcInter::mouseMoveEvent(QMouseEvent* event)
{
	int hoverIndex = -1;
	for (size_t i = 0; i < m_rects.size(); i++)
	{
		if (m_rects[i].contains(event->pos()))
		{
			this->setCursor(Qt::PointingHandCursor);
			hoverIndex = i;
		}
		else
		{
			this->setCursor(Qt::ArrowCursor);
		}
	}

	if (hoverIndex != m_hoverIndex)
	{
		m_hoverIndex = hoverIndex;
		update();
	}
}

void BondDetailPrcInter::onUpdateUi()
{
	update();
}

